<?php

namespace Addons\MultiImages;
use Common\Controller\Addon;

/**
 * 多图字段扩展插件
 * @author Creatint
 */

    class MultiImagesAddon extends Addon{

        public $info = array(
            'name'=>'MultiImages',
            'title'=>'多图字段扩展',
            'description'=>'扩展一个多图上传字段，提供多图上传功能，版本与OT版本同步',
            'status'=>1,
            'author'=>'Creatint',
            'version'=>'0.4'
        );

        public $admin_list = array(
            'model'=>'Example',		//要查的表
			'fields'=>'*',			//要查的字段
			'map'=>'',				//查询条件, 如果需要可以再插件类的构造方法里动态重置这个属性
			'order'=>'id desc',		//排序,
			'list_grid'=>array( 		//这里定义的是除了id序号外的表格里字段显示的表头名和模型一样支持函数和链接
                'cover_id|preview_pic:封面',
                'title:书名',
                'description:描述',
                'link_id|get_link:外链',
                'update_time|time_format:更新时间',
                'id:操作:[EDIT]|编辑,[DELETE]|删除'
            ),
        );

        public function install(){
            return true;
        }

        public function uninstall(){
            return true;
        }

        //实现的FieldType钩子方法
        public function addFieldType($type){
            $config = $this->getConfig();
            if(!empty($config['typeName'])){
                $type[$config['typeName']] = array('多图上传','varchar(255) NOT NULL');
            }
            //dump($type);
            return $type;
        }

        //后台编辑表单扩展方法
        public function addForm(&$params = array()){
            $config = $this->getConfig();
            if($config['typeName'] == $params['typeName']){

                if(!empty($params['images_ids'])){
                    $this->assign('images_ids',$params['images_ids']);

                    $ids = explode(',',$params['images_ids']);

                    if(!empty($ids)){
                        $map = array();
                        $map['id'] = array('in',$ids);
                        $Picture = D("Picture");
                        $images = $Picture->where($map)->field("`id`,`path`")->select();
                        //dump($images);
                        //echo $Picture->getLastSql();
                        $this->assign("images",$images);
                    }

                }else{
                    //echo '空';
                    $this->assign('images_ids',null);
                    $this->assign("images",null);
                }
                $this->assign("fieldName",$params['fieldName']);
                unset($params);
                $this->display("multiImagesForm");
            }
        }

    }